import * as echarts from '../../ec-canvas/echarts.min';
let chart = null;
var utils = require('../../utils/util.js');
var httpUrl = getApp().globalData.httpUrl;
var amountData="";
var max = "";

var interval="";
function initChart(canvas, width, height) {
    chart = echarts.init(canvas, null, {
        width: width,
        height: height
    });
    canvas.setChart(chart);
}
Page({
    data: {
        ec: {
            onInit: initChart,
        },
        maxlength: 500,
        inputlength: 0,
        formatData: "",
        data: "",
        ishide: false,
        isarray: false,
    },
    onLoad: function(option) {
        var that=this;
        this.setData({
            datainfo: option.datainfo,
            userAvatarUrl: wx.getStorageSync("userInfo").avatarUrl,
            nickName: wx.getStorageSync("userInfo").nickName
        })
        var format = utils.formatData(new Date());
        var arr = [];
        for (var i = 2018; i <= format.substr(0, 4); i++) {
            arr.push(i + "上学期");
            arr.push(i + "下学期")
        }
        that.setData({
            array: arr
        })
        console.log(option.time);
        if (option.time.substr(5, 2) > 7) {
            this.setData({
                data: option.time.substr(0, 4) + "下学期"
            })
        } else {
            this.setData({
                data: option.time.substr(0, 4) + "上学期"
            })
        }
        this.setData({
            datatitle: option.time.substr(0, 10),
            forstryear: format.substr(0, 4) + ".5",
            strInfo: this.data.data
        })
        this.sontrastInfo(this.data.data);
        this.listArticle();
    },
    //对比标准数据
    sontrastInfo: function(data) {
        var that = this;
        utils.http(httpUrl + "/mobile/member/my_stand", {
            date: data
        }, function(res) {
            if (res.state == 1) {
                var end_data = res.datas.end_data;
                var start_data = res.datas.start_data;
                var type_list = res.datas.type_list;
                var end_amount = "学期末 " + res.datas.end_ave+"分" // 学期末
                var start_amount = "学期初 " + res.datas.start_ave+ "分"// 学期初 
                 amountData = [start_amount, end_amount];
                console.log(that.data.datainfo);
                
                if(that.data.datainfo==1){
                    max=5;
           
                    interval=1;
                }else{
                    max=100;
                   
                    interval=10;
                }
                    that.setData({
                        ishide: false
                    })
                var option = {
                    // tooltip: {
                    //     trigger: 'axis'
                    // },
                    title: {
                        textStyle: {
                            fontSize: 10
                        }
                    },
                    legend: {
                        data:amountData,
                        left:20
                    },
                    toolbox: {
                        show: true,
                    },
                    calculable: true,
                    xAxis: [{
                        type: 'category',
                        data: type_list,
                        // data: ['1', '2', '3', '4', '5', '6'],
                        axisLabel: {
                            interval: 0,
                            rotate: 25,
                            show: true,
                            textStyle: {
                                color: '#333333',
                                fontSize: 10
                            }
                        }

                    },

                    ],
                    yAxis: [{
                        type: 'value',
                        interval: interval,
                        max:max,
                        min:0,
                    }],
                    series: [{
                        name: start_amount,
                        type: 'bar',
                        data: start_data,
                        itemStyle: {
                            normal: {
                                color: '#43cb83'
                            }
                        }
                    },
                    {
                        name: end_amount,
                        type: 'bar',
                        data: end_data,
                        itemStyle: {
                            normal: {
                                color: '#1f5dea'
                            }
                        }
                    }
                    ]
                };

                chart.setOption(option);
                

            } else {
        that.setData({
            ishide: true
        }) 
                // utils.message(res.datas);
            }
        })
    },
    //对比标准新闻
    listArticle:function(e){
        var that=this;
        utils.http(httpUrl +"/mobile/member/my_person_article",{},function(res){
            if (res.state==1){
                that.setData({
                    datas:res.datas
                })
            }else{
                utils.message(res.datas)
            }

        })
    },
    //日期选择
    bindPickerChange(e) {
        console.log('picker发送选择改变，携带值为', e)
        this.setData({
            index: e.detail.value,
            isarray: true,
        })
        this.sontrastInfo(this.data.array[e.detail.value]);
    }, 
    //slider4change 推拽进度条实现那一年
    // slider4change: function(e) {
    //     this.setData({
    //         value: e.detail.value
    //     })
    //     var str = e.detail.value.toString();
    //     if (str == 2005.5) {
    //         console.log("ksdjfkldsjfkl");
    //         this.setData({
    //             strInfo: ""
    //         })
    //     } else if (str.length > 4) {
    //         this.setData({
    //             strInfo: str.substr(0, 4) + "下学期"
    //         })
    //     } else {
    //         this.setData({
    //             strInfo: str.substr(0, 4) + "上学期"
    //         })
    //     }
    //     this.sontrastInfo(this.data.strInfo);
    // },
    //进入对比标准推送页
    oNpushTap: function(e) {
        wx.navigateTo({
            url: '../contraststandards/push/push?id=' + e.currentTarget.dataset.id,
        })
    },

    //输入文本框获取长度
    bindTextAreaBlur: function(e) {
        var e = e.detail.value;
        this.setData({
            inputlength: e.length
        })
    },
    //意见反馈
    formSubmit: function (e) {
        var that = this;
        utils.http(httpUrl + "/mobile/common/feedback", {
            content: e.detail.value.textarea,
            type:1
        }, function (res) {
            if (res.state == 1) {
                wx.showToast({
                    title: '反馈成功',
                })
            } else {
                utils.message(res.datas);
            }
        })
    },
    onReady() {
        setTimeout(function() {
            // 获取 chart 实例的方式
            //console.log(chart)
        }, 2000);
    }

});